﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using Autofac.Integration.Mvc;
using System.Reflection;
using System.Web.Mvc;
using LigerMVC.Common.MVC;
using LigerMVC.Common;

namespace LigerMVC.WebSite
{
    public class AppConfiger
    {
        public static void Init()
        {
            ConfigInit();

            //请求处理
            ModelBinders.Binders.Add(typeof(QuerySimpleDescriptor), new QuerySimpleDescriptorBinder());

        }

        public static void ConfigInit()
        {
            var builder = new ContainerBuilder();

            //可改成配置的方式
            var assemFullNames = new string[]{
                "LigerMVC.Business.Core",
                "LigerMVC.DataAccess",
                "LigerMVC.Web.Application"
            };

            foreach (var assemFullName in assemFullNames)
            {
                var assem = Assembly.Load(assemFullName);
                if (assemFullName.StartsWith("LigerMVC.Business."))
                {
                    builder.RegisterAssemblyTypes(assem)
                   .Where(a => a.FullName.EndsWith("Business", StringComparison.CurrentCultureIgnoreCase))
                   .AsImplementedInterfaces();
                }
                if (assemFullName.StartsWith("LigerMVC.DataAccess", StringComparison.CurrentCultureIgnoreCase))
                {
                    builder.RegisterAssemblyTypes(assem)
                    .Where(a => a.FullName.EndsWith("DataAccess", StringComparison.CurrentCultureIgnoreCase))
                    .AsImplementedInterfaces();
                }
                if (assemFullName.StartsWith("LigerMVC.Web.Application",StringComparison.CurrentCultureIgnoreCase))
                {
                    builder.RegisterControllers(assem);
                }
            }  

         
            var contain = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(contain));
        }
    }
}